//
//  ComboView.m
//  Project two framework
//
//  Created by rimi on 16/7/23.
//  Copyright © 2016年 张峥. All rights reserved.
//

#import "ComboView.h"
#import "ComboViewCell.h"
#import "ComboViewFlowLayout.h"
#import "ZPZNetRequest+MealsList.h"
#import "ZPZNetRequest.h"
#import "LStringUtils.h"
#import "AFNetworking.h"
#import "ComboTableViewCell.h"
#import "SetMealView.h"
#import "SingleView.h"

@interface ComboView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong)UICollectionView *collection;
@property(nonatomic,strong)NSMutableArray *array;
@property(nonatomic,strong)UIView *lietBtnView;
@property(nonatomic,strong)ComboViewFlowLayout *comboViewFlowLayout;

@property(nonatomic,strong)SetMealView *setMealView;
@property(nonatomic,strong)NSMutableArray *btnArray;

@end


@implementation ComboView
-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
//        self.backgroundColor = [UIColor whiteColor];
         self.frame = CGRectMake(45, 120, 825, 580);
        [self collection];

        [self initializeDataSource];
        
        
    }
    
    return self;
}

#pragma mark --详情点击事件
-(void)clickDetailsBtn:(UIButton *)sender{
    
    self.setMealView = [SetMealView showOtherView];
    [ZPZNetRequest getMealsListPageSize:@"10" PageNum:@"1" UserId:@"22" comletion:^(id object, BOOL success) {
//        NSLog(@"11111111111111%@",object);
        if (!success) {
            return ;
        }
        //这里就可以用tag值来取得按钮的下标
        _setMealView.nameLabel.text = object[@"content"][sender.tag][@"name"];
        NSString *str1 = [NSString stringWithFormat:@"套餐介绍:%@",object[@"content"][sender.tag][@"info"]];
        _setMealView.label_1.text = str1;
        //自适应高度
        CGRect rect = [_setMealView.label_1.text boundingRectWithSize:CGSizeMake(630, 500) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil];
        _setMealView.label_1.frame = CGRectMake(50, CGRectGetMaxY(_setMealView.nameLabel.frame) + 50, 750, rect.size.height);
        _setMealView.label_2.text = @"推荐业务:";
        //自适应高度
        CGRect rect1 = [_setMealView.label_2.text boundingRectWithSize:CGSizeMake(630, 500) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil];
        _setMealView.label_2.frame = CGRectMake(50, CGRectGetMaxY(_setMealView.label_1.frame) + 10, 650, rect1.size.height);
        _btnArray = [[NSMutableArray alloc]initWithArray:object[@"content"][sender.tag][@"summaryFs"] ];
        if (_btnArray.count >= 1) {
            NSString *str2 = [NSString stringWithFormat:@"1.%@",_btnArray[0][@"info"]];
            _setMealView.label_3.text = str2;
            //自适应高度
            CGRect rect3 = [_setMealView.label_3.text boundingRectWithSize:CGSizeMake(630, 500) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil];
            _setMealView.label_3.frame = CGRectMake(50, CGRectGetMaxY(_setMealView.label_2.frame) + 10, 650, rect3.size.height);
        }
        if (_btnArray.count >= 2) {
            NSString *str4 = [NSString stringWithFormat:@"2.%@",_btnArray[1][@"info"]];
            _setMealView.label_4.text = str4;
            //自适应高度
            CGRect rect3 = [_setMealView.label_4.text boundingRectWithSize:CGSizeMake(630, 500) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil];
            _setMealView.label_4.frame = CGRectMake(50, CGRectGetMaxY(_setMealView.label_3.frame) + 10, 650, rect3.size.height);
        }
        if (_btnArray.count >= 3) {
            NSString *str5 = [NSString stringWithFormat:@"3.%@",_btnArray[2][@"info"]];
            _setMealView.label_5.text = str5;
            //自适应高度
            CGRect rect3 = [_setMealView.label_5.text boundingRectWithSize:CGSizeMake(630, 500) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil];
            _setMealView.label_5.frame = CGRectMake(50, CGRectGetMaxY(_setMealView.label_4.frame) + 10, 650, rect3.size.height);
        }

        
    }];

    //保存选购
    [_setMealView.preserveBtn addTarget:self action:@selector(clickPreserveBtn:) forControlEvents:UIControlEventTouchUpInside];
    //申请办理
    [_setMealView.applyForBtn addTarget:self action:@selector(clickApplyForBtn:) forControlEvents:UIControlEventTouchUpInside];
    
}
    //保存选购
-(void)clickPreserveBtn:(UIButton *)sender{
    [SingleView showOtherView];

}

    //申请办理
-(void)clickApplyForBtn:(UIButton *)sender{


}

#pragma mark --集合视图数据源

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _array.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *ID = [NSString stringWithFormat:@"cellID%ld",[indexPath row]];
    ComboViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    [cell.detailsBtn addTarget:self action:@selector(clickDetailsBtn:) forControlEvents:UIControlEventTouchUpInside];
 
    
    cell.label.text = _array[indexPath.row][@"name"];
    /***********************************************/
    //给按钮一个tag值,在上面的点击事件里面才能获得按钮的下标
    /***********************************************/
    cell.detailsBtn.tag = indexPath.row;
    
    NSArray *array = _array[indexPath.row][@"summaryFs"];
    if (array.count  >= 1) {
        NSString *string = [NSString stringWithFormat:@"1.%@", array[0][@"name"]];
         cell.label2.text =  string;
//        cell.label3.text = _array[indexPath.row][@"summaryFs"][1][@"name"];
    }
    if (array.count >= 2) {
        NSString *string = [NSString stringWithFormat:@"2.%@", array[1][@"name"]];
        cell.label3.text =  string;

    }
    if (array.count >= 3) {
        NSString *string = [NSString stringWithFormat:@"3.%@", array[2][@"name"]];
        cell.label4.text =  string;
    }
    
    
    return cell;
}



#pragma mark --网络请求

-(void)initializeDataSource{
    [ZPZNetRequest getMealsListPageSize:@"10" PageNum:@"1" UserId:@"22" comletion:^(id object, BOOL success) {
//        NSLog(@"11111111111111%@",object);
        if (success) {
            _array = [NSMutableArray arrayWithArray:object[@"content"]];
            
            [_collection reloadData];
        }
    }];

}



-(UICollectionView *)collection{
    if (_collection == nil) {
//        //初始化一个布局
//        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
//        //设置间距
//        flowLayout.minimumLineSpacing = 60;
//        //滚动方向
//        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//        //设置格子的大小
//        flowLayout.itemSize = CGSizeMake(335, 500);
        _comboViewFlowLayout = [[ComboViewFlowLayout alloc]init];
        
        //横向移动
        _comboViewFlowLayout.scrollDirection =UICollectionViewScrollDirectionHorizontal;
        _comboViewFlowLayout.itemSize = CGSizeMake(335, 500);
        
        
        _collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) collectionViewLayout:_comboViewFlowLayout];
        _collection.backgroundColor = [UIColor clearColor];
//        _collection.
        //注册cell
        for (int i = 0; i< 9; i++) {
            [_collection registerClass:[ComboViewCell class] forCellWithReuseIdentifier:[NSString stringWithFormat:@"cellID%d",i]];
        }
        
        _collection.delegate = self;
        _collection.dataSource = self;
        
//        [self initializeDataSource];
        [self addSubview:_collection];
    }



    return _collection;

}












@end
